const Error = require('./error')
const storage = require('./storage.js')
const request = require('./request')
const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isArray
} = require('./dataType.js')

const maintenanceGroup = class maintenanceGroup {

    constructor() {}

    get(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MRG.GET_MRG.url,
                MS._config.parameters.MRG.GET_MRG.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if(!isUndefined(options)) {
            if(!isUndefined(options.moId) && isString(options.moId)) {
                params.moId = options.moId;
            }
            if(!isUndefined(options.name) && isString(options.name)) {
                params.name = options.name;
            }
            if(!isUndefined(options.status) && isBoolean(options.status)) {
                params.status = options.status;
            }
            if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum;
            }
            if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MRG.QUERY_MRG.url,
                MS._config.parameters.MRG.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    save(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.moId) && isString(options.moId)) {
                params.moId = options.moId;
            }
            if(!isUndefined(options.name) && isString(options.name)) {
                params.name = options.name;
            }
            if(!isUndefined(options.synopsis) && isString(options.synopsis)) {
                params.synopsis = options.synopsis;
            }
            if(!isUndefined(options.status) && isBoolean(options.status)) {
                params.status = options.status;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MRG.SAVE_MRG.url,
                MS._config.parameters.MRG.SAVE_MRG.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    remove(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MRG.REMOVE_MRG.url,
                MS._config.parameters.MRG.REMOVE_MRG.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    loadEmployee(groupId) {
        let params = {
            groupId: groupId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MRG.REMOVE_MRG.url,
                MS._config.parameters.MRG.REMOVE_MRG.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    appendEmployee(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.groupId) && isString(options.groupId)) {
                params.groupId = options.groupId;
            }
            if(!isUndefined(options.employeeId) && isString(options.employeeId)) {
                params.employeeId = options.employeeId;
            }
            if(!isUndefined(options.isHead) && isBoolean(options.isHead)) {
                params.isHead = options.isHead;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MRG.APPEND_EMPLOYEE.url,
                MS._config.parameters.MRG.APPEND_EMPLOYEE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    removeEmployee(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MRG.REMOVE_EMPLOYEE.url,
                MS._config.parameters.MRG.REMOVE_EMPLOYEE.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    setEmployeeHead(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MRG.SET_EMPLOYEE_HEAD.url,
                MS._config.parameters.MRG.SET_EMPLOYEE_HEAD.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = maintenanceGroup